package ui.panel.setup.subsidiary;

import cerebrum.common.CommonConst;
import cerebrum.entity.FontCacheEntity;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.StageStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ui.factory.SingletonFactory;
import ui.util.Window;

import java.io.IOException;

/**
 * 视频壁纸 ->设置屏保时间
 */
public class ScreenSaverTimeStage {
    private Logger log = LoggerFactory.getLogger(ScreenSaverTimeStage.class);
    private static ScreenSaverTimeStage instance = null;
    private Pane root = new Pane();
    private int index = 0;
    private int[] num = {1, 3, 5, 10};

    //调用单例工厂
    public static ScreenSaverTimeStage getInstance() {
        if (instance == null) {
            instance = SingletonFactory.getWeakInstace(ScreenSaverTimeStage.class);
        }
        return instance;
    }


    public Pane getRootPane() {

//        pane.setStyle("-fx-background-color: TRANSPARENT");
        root.setOpacity(0.8);
        //外层轮廓
        Pane pane1 = new Pane();
        pane1.setPrefWidth(310);
        pane1.setPrefHeight(80);
        pane1.setLayoutX(110);
        pane1.setLayoutY(60);
        pane1.setStyle("-fx-background-color: #f07c82");
        pane1.setBorder(new Border(new BorderStroke(Color.rgb(238, 162, 164), BorderStrokeStyle.SOLID, null, new BorderWidths(10))));

        //数字
        Label numLbl = new Label();
        numLbl.setText(String.valueOf(num[index]));
        numLbl.setLayoutX(123);
        numLbl.setLayoutY(13);
        try {
            numLbl.setFont(Font.loadFont(FontCacheEntity.class.getResource("/fxml/font/FZYongKTJF_Da.TTF").openStream(), 50));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //分钟/s
        Label mLbl = new Label();
        mLbl.setText("分钟/s");
        mLbl.setLayoutX(180);
        mLbl.setLayoutY(45);

        //减 按钮
        Button button = new Button();
        button.setStyle("-fx-shape: 'M512 6.144c-279.552 0-506.88 227.328-506.88 506.88s227.328 506.88 506.88 506.88 506.88-227.328 506.88-506.88-227.328-506.88-506.88-506.88z m0 923.136c-229.888 0-416.256-186.88-416.256-416.256 0-229.888 186.88-416.256 416.256-416.256 229.888 0 416.256 186.88 416.256 416.256s-186.88 416.256-416.256 416.256z M743.424 462.336H280.576c-28.16 0-50.688 22.528-50.688 50.688 0 27.648 22.528 50.688 50.688 50.688h462.848c28.16 0 50.688-22.528 50.688-50.688s-22.528-50.688-50.688-50.688z';");
        button.setPrefHeight(40);
        button.setPrefWidth(40);
        button.setLayoutX(35);
        button.setLayoutY(12);
        button.setPadding(new Insets(20));
        button.setOnMouseClicked(e -> {
            int numReduce = getNumReduce();
            if (numReduce > 9) {
                numLbl.setLayoutX(103);
            } else if (numReduce < 10) {
                numLbl.setLayoutX(123);
            }
            numLbl.setText(String.valueOf(numReduce));
        });

        //加按钮
        Button button2 = new Button();
        button2.setStyle("-fx-shape: 'M511.448 66.205c-245.315 0-444.905 199.59-444.905 444.906 0 245.318 199.59 444.907 444.906 444.907 245.318 0 444.907-199.59 444.907-444.907 0-245.316-199.59-444.906-444.908-444.906z m0 834.198c-214.659 0-389.292-174.63-389.292-389.292 0-214.66 174.633-389.293 389.292-389.293 214.66 0 389.294 174.633 389.294 389.293 0 214.661-174.634 389.292-389.294 389.292z m166.84-417.099H539.257V344.271c0-15.37-12.438-27.806-27.808-27.806s-27.807 12.438-27.807 27.806v139.033H344.611c-15.372 0-27.808 12.438-27.808 27.807 0 15.342 12.437 27.806 27.808 27.806h139.03V677.95c0 15.342 12.438 27.806 27.807 27.806 15.371 0 27.808-12.464 27.808-27.806V538.917h139.031c15.372 0 27.808-12.464 27.808-27.806 0.001-15.37-12.435-27.807-27.807-27.807z'");
        button2.setPrefHeight(40);
        button2.setPrefWidth(40);
        button2.setPadding(new Insets(20));
        button2.setLayoutX(225);
        button2.setLayoutY(12);
        button2.setOnMouseClicked(e -> {
            int numAdd = getNumAdd();
            if (numAdd > 9) {
                numLbl.setLayoutX(103);
            } else if (numAdd < 10) {
                numLbl.setLayoutX(123);
            }
            numLbl.setText(String.valueOf(numAdd));
        });


        Rectangle rectangle1 = new Rectangle();
        rectangle1.setFill(Paint.valueOf("#f07c82"));
        rectangle1.setX(380);
        rectangle1.setY(10);
        rectangle1.setWidth(60.0);
        rectangle1.setHeight(60.0);
        rectangle1.setRotate(130);

        Rectangle rectangle2 = new Rectangle();
        rectangle2.setFill(Paint.valueOf("#f07c82"));
        rectangle2.setX(430);
        rectangle2.setY(10);
        rectangle2.setWidth(60.0);
        rectangle2.setHeight(60.0);
        rectangle2.setRotate(130);

        Rectangle rectangle3 = new Rectangle();
        rectangle3.setFill(Paint.valueOf("#f07c82"));
        rectangle3.setX(380);
        rectangle3.setY(10);
        rectangle3.setWidth(110.0);
        rectangle3.setHeight(65.0);
        rectangle3.setVisible(false);
        rectangle3.setStrokeWidth(10);
        rectangle3.setStroke(Color.rgb(238, 162, 164));


        Shape shape = Shape.union(rectangle1,rectangle2);
        shape.setFill(Paint.valueOf("#f07c82"));
        shape.setStrokeWidth(5);
        shape.setStroke(Color.rgb(238, 162, 164));

        shape.setOnMouseMoved(event -> {
            shape.setVisible(false);
            rectangle3.setVisible(true);
                }
        );

        rectangle3.setOnMouseExited(e -> {
            shape.setVisible(true);
            rectangle3.setVisible(false);
        });


        Text label = new Text();
        label.setText("确   定");
        label.setFill(new LinearGradient(0, 0, 1, 2, true, CycleMethod.REPEAT, new
                Stop[]{new Stop(0, Color.AQUA), new Stop(0.5f, Color.RED)}));

        try {
            label.setFont(Font.loadFont(FontCacheEntity.class.getResource("/fxml/font/FZYongKTJF_Da.TTF").openStream(), 30));
        } catch (IOException e) {
            e.printStackTrace();
        }
        label.setLayoutX(395);
        label.setLayoutY(52);

        pane1.getChildren().addAll(button, numLbl, button2, mLbl, shape,rectangle3,label);
        root.getChildren().add(pane1);

        return root;
    }

    private int getNumAdd() {
        if (index < num.length - 1) {
            index++;
            return num[index];
        }
        return num[num.length - 1];
    }

    private int getNumReduce() {
        if (index > 0) {
            index--;
            return num[index];
        }
        return num[0];
    }
}
